#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <MyLibrary.au3>
#include <String.au3>
#include <Date.au3>
#include <array.au3>
#Include <Misc.au3>

HotkeySet("{ESC}","terminate")

Opt("WinTitleMatchMode", 4)

GUICreate("TEST", 250, 250,0,0,-1,$WS_EX_TOPMOST)
 
GUICtrlCreateLabel("Facing:", 5,5, 95, 50)
dim $ctrlfacing = GUICtrlCreateLabel("0", 80,5, 70, 18 )

GUICtrlCreateLabel("New Facing:", 5, 25, 95, 50)
dim $ctrlnewfacing = GUICtrlCreateLabel("0", 80, 25, 70, 18 )

GUICtrlCreateLabel("Points:", 5, 50, 95, 20)
dim $ctrlpoints = GUICtrlCreateLabel("0", 80, 50, 70, 18 )

GUICtrlCreateLabel("Buffer:", 5, 75, 95, 20)
dim $ctrlbuffer = GUICtrlCreateLabel("0", 80, 75, 70, 18 )

GUICtrlCreateLabel("Direction:", 5, 75, 95, 20)
dim $ctrldirection = GUICtrlCreateLabel("SAME", 80, 75, 70, 18 )

GUICtrlCreateLabel("Status:", 5, 100, 95, 20)
dim $ctrlstatus = GUICtrlCreateLabel("NONE", 80, 100, 70, 18 )

GUICtrlCreateLabel("Time:", 5, 125, 95, 20)
dim $ctrltime = GUICtrlCreateLabel("0", 80, 125, 70, 18 )

GUISetState()
dim $time_taken = 0
dim $reset_time = true
Global $WinTitle = "World of Warcraft"
Global $WinSize = GetWinSize($WinTitle)
Global $Lite = ObjCreate("SERFLITEMAX20.Core.Main")
Global $direction
Global $points
$Lite.Start

while not $Lite.HasStarted
wend

WinActivate($WinTitle)
WinWaitActive($WinTitle)

$Lite.Player.Update

local $facing = $Lite.PlayerFacing;
local $buffer = 0.065

GUICtrlSetData($ctrlfacing,$facing)
GUICtrlSetData($ctrlbuffer,$buffer)

send("aaaaaaaaaaaaaaaaaa")
sleep(3000)
while True
	$Lite.Player.Update
	local $next = $Lite.PlayerFacing;

	
	$direction = $Lite.CalcDirection($facing,$next)
	$points = $Lite.CalcTurnAmount($facing,$next)
	GUICtrlSetData($ctrlnewfacing,$next)
	GUICtrlSetData($ctrlpoints,$points)
	GUICtrlSetData($ctrldirection,$direction)
	dim $status = true
	
	while $status

		if ($direction = "SAME") Then
			$status = False
		endif
		if ($points < $buffer) then
			$status = $status and False
		endif
		 
		 ;msgbox(0, $status, $status )
		if $status then
			if $reset_time Then
				$reset_time = false;
				$time_taken = 0;
			endif
			;local $amount = 
			GUICtrlSetData($ctrlstatus,"TURNING...")
			if $direction = "LEFT" Then
				$moveCount = Floor($points / 0.06)
				while $moveCount > 0
					send("aaa")
					sleep(55)
					$time_taken=$time_taken+55
					$moveCount = $moveCount - 1
					GUICtrlSetData($ctrltime,$time_taken)
				wend
				 
				GUICtrlSetData($ctrlstatus,"TURNING...LEFT")
			endif
			if $direction = "RIGHT" Then
				$moveCount = Floor($points / 0.06)
				while $moveCount > 0
					send("ddd")
					sleep(55)
					$time_taken = $time_taken + 55
					$moveCount = $moveCount - 1
					GUICtrlSetData($ctrltime,$time_taken)
				wend
				 
				GUICtrlSetData($ctrlstatus,"TURNING...RIGHT")
			endif
			
			 
			
			$Lite.Player.Update
			$next = $Lite.PlayerFacing
			 
			$direction = $Lite.CalcDirection($facing,$next)
			$points = $Lite.CalcTurnAmount($facing,$next)
			
			GUICtrlSetData($ctrlnewfacing,$next)
			GUICtrlSetData($ctrlpoints,$points)
			GUICtrlSetData($ctrldirection,$direction)
		endif
	wend
	GUICtrlSetData($ctrlstatus,"DONE!")
	$reset_time=true
	sleep(50)
wend
;MsgBox(0,"test",$direction )
;sleep(1000)

Exit

 


func terminate()
    sleep(100)
 
;send("{a up}")
        exit 0
EndFunc ;==>Terminate